package com.shop.cereshop.admin.service.recommend.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.admin.dao.recommend.CereRecommendCommentDAO;
import com.shop.cereshop.admin.param.recommend.RecommendStatusPageParam;
import com.shop.cereshop.admin.param.recommend.RecommendTrendsParam;
import com.shop.cereshop.admin.service.history.impl.CereWebHistoryServiceImpl;
import com.shop.cereshop.admin.service.recommend.CereRecommendCommentService;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shop.cereshop.commons.domain.recommend.CereRecommendComment;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class CereRecommendCommentServiceImpl implements CereRecommendCommentService {
    @Autowired
    private CereRecommendCommentDAO cereRecommendCommentDAO;

    @Autowired
    private CereWebHistoryServiceImpl cereWebHistoryService;
    @Override
    public Page getAll(RecommendTrendsParam recommendTrendsParam,HttpServletRequest request) {
//        分页查询动态评论中的所有评论
        PageHelper.startPage(recommendTrendsParam.getPage(), recommendTrendsParam.getPageSize());
        List<CereRecommendComment> userAllComments = cereRecommendCommentDAO.getUserAllComments(recommendTrendsParam);
        PageInfo<CereRecommendComment> cereRecommendCommentPageInfo = new PageInfo<>(userAllComments);
        return new Page<>( cereRecommendCommentPageInfo.getList(),  cereRecommendCommentPageInfo.getTotal());
    }

    @Override
    @Transactional
    public Integer deleteRecommendComment(Long commentId, HttpServletRequest request ) {

        String action ="";
        String result ="";

//        更新动态的评论数量
        int number= cereRecommendCommentDAO.deleteComment(commentId);

        CerePlatformUser user  =(CerePlatformUser)request.getAttribute("user");

        if (number>0){
            result= "成功";
        }
        else {
            result= "失败";
        }
            action= user.getName()+"删除了"+"动态圈评论id为"+commentId;

        cereWebHistoryService.addHistory(user.getPlatformUserId(),user.getName(),user.getPhone(),user.getAvatar()==null ?null:user.getAvatar(),action,result,2);

        return number;
    }

    @Override
    public Integer updateStatue(RecommendStatusPageParam param,HttpServletRequest request) {

        String result ="";
        String action ="";

        Integer number = cereRecommendCommentDAO.updateStatue(param);

        CerePlatformUser  user  =(CerePlatformUser)request.getAttribute("user");

        if (number>0){
            result= "成功";
        }
        else {
           result= "失败";
        }
        if (param.getStatus()==1){
            action= user.getName()+"隐藏了"+"评论id为"+param.getRecommendCommentId()+"的动态评论";
        }else if (param.getStatus()==0){
           action = user.getName()+"展示了"+"评论id为"+param.getRecommendCommentId()+"的动态评论";
        }

        cereWebHistoryService.addHistory(user.getPlatformUserId(),user.getName(),user.getPhone(),user.getAvatar()==null ?null:user.getAvatar(),action,result,1);

        return number;

    }
}
